#!/bin/python3
# -*- coding: UTF-8
import sys
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.dates as mdates
from matplotlib.ticker import ScalarFormatter
from matplotlib.ticker import LogFormatter
from matplotlib.ticker import MultipleLocator
from matplotlib.ticker import FormatStrFormatter
from matplotlib.font_manager import findfont, FontProperties  
import matplotlib.font_manager as font_manager
import matplotlib.ticker as mtick
import seaborn as sns
from scipy.stats.mstats import gmean

import common



filename = sys.argv[1]
data = common.read_data(filename)

# 获得所有lib名字(除去第一行)
libname = common.uniqx(data[1:])
print (libname)
# 获得所有bench 名字
benchname = common.uniqx(common.filter(data, libname[0]))
print (benchname)
# 获得所有的thread
threadlist = common.uniqx(common.filter(common.filter(data, libname[0]), benchname[0])).astype(int)
threadlist.sort()
threadlist = threadlist.astype(str)
print(threadlist)

# 计算n次执行结果的平均值和标准差
avg_result = []
for lib in libname:
	for bench in benchname:
		for thread in threadlist:
			# 多次执行结果 计算平均值和标准差
			result_data = common.filter(common.filter(common.filter(data, lib), bench), thread).astype(float)
			avg_result.append(np.array([lib, bench, thread, np.mean(result_data).astype(str), np.std(result_data).astype(str)]))
			# print("%s %s %s %.3f %.3f" % (lib, bench, thread, np.mean(result_data), np.std(result_data)))
avg_result = np.array(avg_result)

print(avg_result)
print (avg_result)

print ("------加速比------")
#  所有bench计算几何平均数方便进行快速对比
comparelist = ['bench_sync']
comparelista = ['bench_async']
for idx in range(0, len(comparelist)):
	for lib in libname:
		for thread in threadlist:
			tmpdata0 = common.choose(common.choose(common.choose(avg_result, lib, 0), thread, 2), comparelist[idx], 1)
			gm = gmean(tmpdata0[:, 3].astype(float))
			tmpdata1 = common.choose(common.choose(common.choose(avg_result, lib, 0), thread, 2), comparelista[idx], 1)
			cacgm = gmean(tmpdata1[:, 3].astype(float))
			# print(tmpdata1[:, 3].astype(float)/tmpdata0[:, 3].astype(float))
			accel = cacgm/gm
			print("%s %s %.3f" % (lib, thread, accel))
# 每次结果计算几何平均数再求平均和标准差 --> 几何平均数的平均和分布




# print ("------cac加速比------")
# print ("lib thread accel")
# comparelist = ['ccsync', 'dsmsync', 'hsync', 'flat']
# comparelistcac = ['cacccsyn', 'cacdsmsyn', 'cachsyn', 'cacflat']

# for idx in range(0, len(comparelist)):
# 	for thread in threadlist:
# 		tmpdata0 = common.choose(common.choose(avg_result, comparelist[idx], 0), thread, 2)
# 		gm = gmean(tmpdata0[:, 3].astype(float))
# 		tmpdata1 = common.choose(common.choose(avg_result, comparelistcac[idx], 0), thread, 2)
# 		cacgm = gmean(tmpdata1[:, 3].astype(float))
# 		print(tmpdata1[:, 3].astype(float)/tmpdata0[:, 3].astype(float))
# 		accel = cacgm/gm
# 		print("%s %s %.3f" % (comparelist[idx], thread, accel))